<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/16
 * Time: 13:51
 */

namespace app\bm\controller;


use app\common\model\AuthRule;
use think\Db;
use think\Request;

class Menu extends Common
{

    public function index()
    {
        $AuthRule = new AuthRule();
        $menuTrue = $AuthRule->lists();
        $this->assign([
            'menuTrue'=>$menuTrue
        ]);
        return $this->view->fetch();
    }

    public function create()
    {
        $AuthRule = new AuthRule();
        $menu = $AuthRule->lists();

        $this->assign([
            'menu'=>$menu
        ]);
        return $this->view->fetch();
    }

    public function save(Request $request)
    {
        if($request->isPost()){
            $data = [
                'title'     =>$request->post('title'),
                'name'      =>$request->post('name'),
                'upid'      =>$request->post('upid'),
                'rank'      =>$request->post('rank'),
                'is_show'   =>$request->post('is_show')
            ];

            $res = Db::name('AuthRule')->insert($data);
            if($res){
                return json([
                    'code'=>200,
                    'msg'=>'添加成功'
                ]);
            }

            dump($data);
            exit;

            //这里暂时做独立验证
            $rule = [
                'title'  => 'require|max:50',
                'name'   => 'require',
                'upid'   => 'require',
            ];
            $msg = [
                'title.require' => '菜单名称必须',
                'title.max'     => '名称最多不能超过50个字符',
                'name.require'  => 'URL必须',
                'upid.require'  => '所属分类必须'
            ];

            $data = [
                'title' =>$request->post('title'),
                'name'  =>$request->post('name'),
                'upid'  =>$request->post('upid')
            ];

            $validate   = Validate::make($rule,$msg);
            $result = $validate->check($data);

            if(!$result) {
                dump($validate->getError());
            }
        }
    }

    public function edit(Request $request)
    {
        $id = $request->param('id');
        if($id){
            $info = Db::name('AuthRule')->find($id);
            if($info){
                $AuthRule = new AuthRule();
                $menu = $AuthRule->lists();
                $this->assign([
                    'menu'=>$menu,
                    'info'=>$info
                ]);
                return $this->view->fetch();
            }else{
                return $this->error('数据错误');
            }
        }else{
            return $this->error('参数错误');
        }
    }

    public function update(Request $request)
    {
        if($request->isPost()){
            $id = $request->post('id');
            $data = [
                'title'     =>$request->post('title'),
                'name'      =>$request->post('name'),
                'upid'      =>$request->post('upid'),
                'rank'      =>$request->post('rank'),
                'is_show'   =>$request->post('is_show')
            ];

            $res = Db::name('AuthRule')->where('id',$id)->update($data);
            if($res){
                return json([
                    'code'=>200,
                    'msg'=>'修改成功'
                ]);
            }

            dump($data);
            exit;
        }else{
            return $this->error('非法请求');
        }
    }

    // 删除
    public function delete(Request $request)
    {
        $id = $request->param('id');

        if ( empty($id) ) {
            return $this->error('请选择要操作的数据!');
        }

        $map = array('id' => array('in', $id) );
        if(Db::name('AuthRule')->where($map)->delete()){
            //记录行为 todo...
            return $this->success('删除成功');
        } else {
            return $this->error('删除失败！');
        }
    }
}